package marvin.webapp.audio.rest;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import marvin.music.dto.ClientList;
import marvin.music.dto.pojo.PlayPojo;
import marvin.music.rest.ServiceURIs;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;

public class RestClient {
	private static final String SERVER_URI = "http://localhost:8080/music-server/rest";

	private static RestTemplate restTemplate;
	
	private static void initRestTemplate(){
		if(restTemplate == null){
			ApplicationContext context = new ClassPathXmlApplicationContext(
					"applicationContext.xml");
			restTemplate = (RestTemplate) context.getBean("restTemplate");
		}
	}
	
	public RestClient() {
		initRestTemplate();
	}
	
	public ClientList listConnectedClient(){
		ClientList list = restTemplate.getForObject(SERVER_URI + ServiceURIs.LIST_CLIENTS,
				ClientList.class);
		return list;
	}
	
	public void playResource(String clientName, String path){
		PlayPojo pojo = new PlayPojo();
		pojo.setClient(clientName);
		pojo.setPath(path);
		
		Map<String, String> vars = new HashMap<String, String>();
        vars.put(ServiceURIs.PARAM_CLIENT_NAME, clientName);
        
        String encodedPath = "";
        try {
        	encodedPath = java.net.URLEncoder.encode(path, "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        vars.put(ServiceURIs.PARAM_PATH, encodedPath);
		
		restTemplate.postForObject(SERVER_URI + ServiceURIs.PLAY_WEB_RADIO, pojo, PlayPojo.class, vars);
	}
	
	public void playFile(String clientName, String path){
		PlayPojo pojo = new PlayPojo();
		pojo.setClient(clientName);
		pojo.setPath(path);
		
		Map<String, String> vars = new HashMap<String, String>();
        vars.put(ServiceURIs.PARAM_CLIENT_NAME, clientName);
        
        String encodedPath = "";
        try {
        	encodedPath = java.net.URLEncoder.encode(path, "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        vars.put(ServiceURIs.PARAM_PATH, encodedPath);
        
        String type = path.substring(path.lastIndexOf(".")+1, path.length());
        vars.put(ServiceURIs.PARAM_TYPE, type);
		
		restTemplate.put(SERVER_URI + ServiceURIs.PLAY_FILE, pojo, vars);
	}
	
	public void addToPlayList(String clientName, String path){
		PlayPojo pojo = new PlayPojo();
		pojo.setClient(clientName);
		pojo.setPath(path);
		
		Map<String, String> vars = new HashMap<String, String>();
        vars.put(ServiceURIs.PARAM_CLIENT_NAME, clientName);
        
        String encodedPath = "";
        try {
        	encodedPath = java.net.URLEncoder.encode(path, "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        
        String type = path.substring(path.lastIndexOf(".")+1, path.length());
        
        vars.put(ServiceURIs.PARAM_PATH, encodedPath);
        vars.put(ServiceURIs.PARAM_TYPE, type);
		
        restTemplate.put(SERVER_URI + ServiceURIs.ADD_TO_PLAY_LIST, pojo, vars);
	}
	
	public void pause(String name) {
		restTemplate.put(SERVER_URI + ServiceURIs.STREAM_PAUSE, name, name);
	}
	
	public void stop(String name) {
		restTemplate.put(SERVER_URI + ServiceURIs.STREAM_STOP, name, name);
	}
	
	public void resume(String name) {
		restTemplate.put(SERVER_URI + ServiceURIs.STREAM_RESUME, name, name);
	}
	
	public void next(String name) {
		restTemplate.put(SERVER_URI + ServiceURIs.STREAM_NEXT, name, name);
	}
	
	public void previous(String name) {
		restTemplate.put(SERVER_URI + ServiceURIs.STREAM_PREVIOUS, name, name);
	}

	public void streamPlayList(String name) {
		restTemplate.put(SERVER_URI + ServiceURIs.PLAY_LIST_PLAY, name, name);		
	}

	public void empty(String name) {
		restTemplate.put(SERVER_URI + ServiceURIs.PLAY_LIST_CLEAR, name, name);		
	}
}
